package com.jackrain.nea.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.config.PropertiesConf;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author lzm
 * @date 2017-12-1
 */
@Slf4j
@RestController
@Api(value = "SettingController", description = "设置获取")
public class SettingController {
    @ApiOperation(value = "配置读取", notes = "获取配置")
    @RequestMapping(path = "/p/cs/settings", method = RequestMethod.GET)
    public JSONObject fetchSettings(HttpServletRequest request
            , @RequestParam(value = "configNames") String configNames){
        JSONArray configs =JSON.parseArray(configNames);

        PropertiesConf pconf = ApplicationContextHandle.getBean("propertiesConf");
        HashMap<String, Object> configMaps = new HashMap<>(4);
        configs.forEach(temp->{
            String config = (String)temp;
            String value = pconf.getProperty(config);
            if(value != null) {
                configMaps.put(config, value);
            }
        });

        ValueHolder result = new ValueHolder();
        result.put("code", 0);
        result.put("data", configMaps);
        return result.toJSONObject();
    }
}
